<template>
  <el-dialog v-model="showDialog" title="编辑规格" width="800px" @opened="handleOpened" @closed="handleClosed">
    <el-form ref="formRef" :model="form" :label-width="120">
      <product-name v-model:model-value="form.name"></product-name>
      <product-thumbnail-url v-model:model-value="form.thumbnailUrl"></product-thumbnail-url>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button @click="handleConfirm" type="primary">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {IGoodsPageRow, IStoreProductRow} from "@/api/modules/pointsMall";
import ProductName from "@/components/goodsCenter/addProductFormItem/ProductName.vue";
import ProductThumbnailUrl from "@/components/goodsCenter/addProductFormItem/ProductThumbnailUrl.vue";
import {useEditProduct} from "@/components/goodsCenter/compositions/useEditProduct";
import {IAddGiftPackData} from "@/api/modules/vipCenter";
import {positiveInteger} from "@/utils/commonRegexp";


const props = withDefaults(defineProps<{
  show: boolean,
  rowData: IGoodsPageRow, // 商品行数据
  productRowData: IStoreProductRow, // 规格行数据
  formProps?: string[], // 标识form中的item项都有哪些，会影响初始化时的格式化，提交时的格式化
  giftPackForm: IAddGiftPackData
}>(), {
  formProps: () => [],
})
const emits = defineEmits(['update:show', 'confirm', 'cancel', 'click-submit'])
const showDialog = computed({
  get() {
    return props.show
  },
  set(val) {
    return emits("update:show", val)
  }
})

const {form, formRef, getForm} = useEditProduct()

// 暂时不需要
const formatData = () => {

}

const handleConfirm = () => {
  formRef.value?.validate(async (valid: boolean, fields: any) => {
    if (valid) {
      emits('click-submit', form)
    }
  })
}

const handleCancel = () => {
  showDialog.value = false
}

const handleOpened = () => {
  getForm(props.productRowData)
}

const handleClosed = () => {
  formRef.value?.resetFields()
}

// const stockRules = [{
//   required: true,
//   trigger: 'blur',
//   validator(rule:any, value:any, callback:any) {
//     debugger
//     if (!positiveInteger.test(form.addOrDeductionStockCount + '')) {
//       callback(new Error('规格库存不能为空且需为正整数'))
//     } else {
//       callback()
//     }
//   }
// }]

</script>

<style scoped lang="scss">
@use "@/components/goodsCenter/styles/common.scss";
</style>
